/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id: mac_gen.h 348 2010-11-29 09:01:16Z bgao $ 
#
# $Author: bgao $ 
#
# $Revision: 348 $ 
#
# $Date: 2010-11-29 17:01:16 +0800 (星期一, 29 十一月 2010) $ 
#
# Description: 
#
=============================================================================*/
/*! @file mac_gen.h
 *  @brief This file is an interface of the mac generator module.
 */
#ifndef _MAC_GEN_H_
#define _MAC_GEN_H_

#include "common.h"
#include "buf.h"

/*! frame control field offset */
#define MAC_FRM_TYPE_OFFSET     0
#define MAC_EN_SEC_OFFSET       3
#define MAC_FRM_PEND_OFFSET     4
#define MAC_ACK_OFFSET          5
#define MAC_PAN_ID_COMP_OFFSET  6
#define MAC_DEST_ADDR_M_OFFSET  10
#define MAC_FRM_VER_OFFSET      12
#define MAC_SRC_ADDR_M_OFFSET   14

/*! superframe info offest */
#define MAC_BCN_ORDER_OFFSET        0
#define MAC_SUP_FRM_ORDER_OFFSET    4
#define MAC_FNL_CAP_SLOT_OFFSET     8
#define MAC_BLE_OFFSET              12
#define MAC_PAN_COORD_OFFSET        14
#define MAC_ASSOC_PMIT_OFFSET       15

/*! capability info offest */
#define ALTNT_PAN_COORD_OFFSET   0
#define DEV_TYPE_OFFSET          1
#define PW_SRC_OFFSET            2
#define REC_ON_WHEN_IDLE_OFFSET  3
#define SEC_CAP_OFFSET           6
#define ALLOC_ADDR_OFFSET        7

/*! definition of MAC command id */
typedef enum cmd_id_t_ {
    CMD_ASSOC_REQ      = 0x01,
    CMD_ASSOC_RESP     = 0x02,
    CMD_DIS_ASSOC      = 0x03,
    CMD_DATA_REQ       = 0x04,
    CMD_PAN_ID_CNFLICT = 0x05,
    CMD_ORPHAN         = 0x06,
    CMD_BCN_REQ        = 0x07,
    CMD_COORD_RALMT    = 0x08,
    CMD_GTS_REQ        = 0x09
} cmd_id_t;

/*! definition of MAC frame type values */
typedef enum mac_frm_type_value_t_ {
    FRM_TYPE_BCN    = 0b0,
    FRM_TYPE_DATA   = 0b1,
    FRM_TYPE_ACK    = 0b10,
    FRM_TYPE_CMD    = 0b11,
} mac_frm_type_value_t;

/*! definition of MAC frame version value */
typedef enum _mac_frm_ver_value_t {
    V_2003 = 0b0,
    V_2006 = 0b1,
} mac_frm_ver_value_t;

/*! definition of MAC frame control field */
typedef struct _mac_frm_ctrl_t {
    mac_frm_type_value_t frm_type;       /*!< frmame type */
    uint8 en_sec;                        /*!< enable security */
    uint8 frm_pend;                      /*!< frmame pending */
    uint8 ack;                           /*!< acknowlegement request */
    uint8 pan_id_comp;                   /*!< PAN ID compression */
    mac_addr_m_value_t dest_addr_m;      /*!< Dest Addressing Mode */
    mac_frm_ver_value_t frm_ver;         /*!< frmame version */
    mac_addr_m_value_t src_addr_m;       /*!< source addressing mode */
} mac_frm_ctrl_t;

/*! definition of MAC header field */
typedef struct _mac_hdr_t {
    mac_frm_ctrl_t ctrl;          /*!< frmame control field */
    uint8          dsn;           /*!< sequence number */
    uint16         dest_pan_id;   /*!< destination PAN ID */
    addr_t         dest_addr;     /*!< destination address */
    uint16         src_pan_id;    /*!< source PAN ID */
    addr_t         src_addr;      /*!< source address */
} mac_hdr_t;

/*! definition of MAC super frame field */
typedef struct mac_sup_frm_t_ {
    uint8 bcn_order;             /*!< beacon order */
    uint8 sup_frm_order;         /*!< superframe order */
    uint8 fnl_cap_slot;          /*!< final CAP slot */
    uint8 ble;                   /*!< batterry life extension */
    uint8 pan_coord;             /*!< PAN coordinator */
    uint8 assoc_pmit;            /*!< association permit */
} mac_sup_frm_t;

/*! definition of MAC capability informtion filed */
typedef struct mac_cap_info_t_ {
    uint8 altnt_pan_coord;       /*!< alternate PAN coordinator */
    uint8 dev_type;              /*!< device type */
    uint8 pw_src;                /*!< power source */
    uint8 rec_on_when_idle;      /*!< receiver on when idle */
    uint8 sec_cap;               /*!< security capability */
    uint8 alloc_addr;            /*!< allocation address */
} mac_cap_info_t;

/*! definition of MAC acknowledgement frame */
typedef struct mac_frm_ack_t_ {
    uint8 dsn;
    uint8 pend;
} mac_frm_ack_t;

/*! definition of MAC beacon frame */
typedef struct mac_frm_bcn_t_ {
    addr_t src;
    uint16 pan_id;
    uint8 bsn;
    uint16 sup_frm_value;
} mac_frm_bcn_t;

/*! definition of mac association request frame */
typedef struct mac_frm_assoc_req_t_ {
    /*!< header specific */
    addr_t src;
    addr_t dest;
    uint16 src_pan_id;
    uint16 dest_pan_id;

    uint8 cap_info;
    uint8 dsn;
} mac_frm_assoc_req_t;

/*! definition of MAC association response frame */
typedef struct mac_frm_assoc_resp_t_ {
    addr_t src;
    addr_t dest;
    uint16 short_addr;
    uint8 status;
    uint8 dsn;
    uint16 pan_id;
} mac_frm_assoc_resp_t;

/*! definition of MAC data request frame */
typedef struct mac_frm_data_req_t_ {
    addr_t src;
    addr_t dest;
    uint8 pan_id_comp;
    uint16 src_pan_id;
    uint16 dest_pan_id;
    uint8 dsn;
} mac_frm_data_req_t;

uint16 mac_gen_sup_frm(mac_sup_frm_t *sup_frm);
uint8 mac_gen_cap_info(mac_cap_info_t *cap_info);

uint16 mac_gen_frm_ctrl(mac_frm_ctrl_t *ctrl);
int8 mac_gen_hdr(buf_t *buf, mac_hdr_t *hdr);
int8 mac_gen_ack(buf_t *buf, mac_frm_ack_t *p);
int8 mac_gen_bcn(buf_t *buf, mac_frm_bcn_t *bcn);
int8 mac_gen_assoc_req(buf_t *buf, mac_frm_assoc_req_t *req);
int8 mac_gen_assoc_resp(buf_t *buf, mac_frm_assoc_resp_t *resp);
int8 mac_gen_data_req(buf_t *buf, mac_frm_data_req_t *req);
int8 mac_gen_bcn_req(buf_t *buf, uint8 dsn);

#endif /* _MAC_GEN_H_ */
